﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKSparseDistance.ModelLayer
{
	public class XYZCompare : IEqualityComparer<XYZ>
	{
		private XYZCompare()
		{
		}

		public static XYZCompare GetCompare()
		{
			if (XYZCompare.m_singleTag == null)
			{
				XYZCompare.m_singleTag = new XYZCompare();
			}
			return XYZCompare.m_singleTag;
		}

		public void Reset()
		{
			this.m_lstPointChache = new List<XYZ>();
		}

		public bool Equals(XYZ x, XYZ y)
		{
			return (x == null && y == null) || (x != null && y != null && (x.IsAlmostEqualTo(y) || x.Equals(y)));
		}

		public int GetHashCode(XYZ obj)
		{
			XYZ xyz = this.m_lstPointChache.Find((XYZ k) => k.IsAlmostEqualTo(obj, XYZCompare.m_dtolerance));
			if (xyz != null && xyz.IsAlmostEqualTo(obj, XYZCompare.m_dtolerance))
			{
				return xyz.GetHashCode();
			}
			this.m_lstPointChache.Add(obj);
			return obj.GetHashCode();
		}

		private static XYZCompare m_singleTag = null;

		private static double m_dtolerance = 0.0033;

		private List<XYZ> m_lstPointChache = new List<XYZ>();
	}
}
